package com.apes.hr.transfer.model;

import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;

import com.apes.scm.masterdata.enumeration.model.Enumeration;
import com.apes.scm.masterdata.frame.model.Dept;
import com.apes.scm.masterdata.party.model.PartyRole;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

/**
 * 功能：调动申请 单
 * @author FengJiaJing
 * @create 2018-07-03 11:20
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = TransferApplication.CONST_TABLE_NAME)
public class TransferApplication  extends Model {
    public static final String CONST_TABLE_NAME = "hr_transfer_application";

    @Id
    @Column(name = "id",nullable = false, length = 32)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "调动单号", prefix = "T", padding = 10)
    private String id; //调动单号

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "transfer_type",  referencedColumnName = "id")
    private Enumeration hrTransferType; //调动类型(000102:hrTT_01调动;000103:hrTT_02支援)

    @Column(name = "note", length = 250)
    private String note; //备注

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "dept_id", referencedColumnName = "id")
    private Dept dept; // 部门(用于部门只能查询本部门数据)

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "company_id", referencedColumnName = "id")
    private PartyRole company; //公司

    @Column(name = "state", length = 10, nullable = false)
    private String state; //单据状态： ('create', '调动申请'), ('firstTrial', '调动初审'), ('done', '调动审批'), ('delete', '删除'), ('retreat', '退回'),('repeal','终止')

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "create_uid", referencedColumnName = "id")
    private PartyRole createUid; //创建人

    @Column(name = "first_trial_Date")
    private Date firstTrialDate; //初审日期

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "first_trial_uid", referencedColumnName = "id")
    private PartyRole firstTrialUid; // 初审人

    @Column(name = "approve_date")
    private Date approveDate; //审批日期

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "approve_uid", referencedColumnName = "id")
    private PartyRole approveUid; //审批人

    @Column(name = "delete_date")
    private Date deleteDate; //删除日期

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "delete_uid", referencedColumnName = "id")
    private PartyRole deleteUid; //删除人

    @Column(name = "return_date")
    private Date returnDate; //退回日期

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "return_uid", referencedColumnName = "id")
    private PartyRole returnUid; //退回人

    @OneToMany(mappedBy="transferApplications",cascade=CascadeType.ALL)
    private Set<TransferApplicationItem> transferApplicationItems = new HashSet<>();
    public Set<TransferApplicationItem> getTransferApplicationItems() {
            if (transferApplicationItems == null) return null;
            for (TransferApplicationItem item : transferApplicationItems) {
                item.setTransferApplications(this);
            }
            return transferApplicationItems;
    }
//    public void setTransferApplicationItems(Set<TransferApplicationItem> transferApplicationItems) {
//        this.transferApplicationItems = transferApplicationItems;
//    }
    @Column(name = "doc_number", length = 32)
    @Comment("文号")
    private String docNumber;   //文号

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "send_out_uid", referencedColumnName = "id")
    @Comment("发文人")
    private PartyRole sendOutUid; //发文人

    @Column(name = "send_out_date")
    @Comment("发文日期")
    private Date sendOutDate; //发文日期
}
